package user

import (
	"gitee.com/zhanglibin/gmall/controller/params"
	userModel "gitee.com/zhanglibin/gmall/model/user"
	srvUser "gitee.com/zhanglibin/gmall/service/user"
	"github.com/gin-gonic/gin"
	// "gitee.com/zhanglibin/xlog/log"
	"gitee.com/zhanglibin/gmall/utils/consts"
	"gitee.com/zhanglibin/gmall/utils/response"
	// "fmt"
)

func Login(c *gin.Context) {

	// log.Info("99999999")

	var req params.LoginReqParam

	if err := c.ShouldBind(&req); err != nil {
		response.Failure(c, consts.CurdLoginFailCode, err.Error(), nil)
		return
	}

	u := srvUser.User{LoginParam: &req}

	err := u.Login(c)
	if err != nil {
		response.Failure(c, consts.CurdLoginFailCode, err.Error(), nil)
		return
	}

	// fmt.Println(req.Username,req.Password)

	response.Success(c, nil)
}

func Register(c *gin.Context) {
	var req params.RegisterReqParam

	if err := c.ShouldBind(&req); err != nil {
		response.Failure(c, consts.CurdLoginFailCode, err.Error(), nil)
		return
	}

	var u srvUser.User
	u.RegParam = req
	err := u.Register(c)
	if err != nil {
		response.Failure(c, consts.CurdLoginFailCode, err.Error(), nil)
		return
	}

	response.Success(c, nil)
}

func GetUserInfo(c *gin.Context) {
	var req params.GetUserInfoParam
	if err := c.ShouldBind(&req); err != nil {
		response.Failure(c, consts.CurdLoginFailCode, err.Error(), nil)
		return
	}

	var u srvUser.User
	u.GetUserInfoParam = req

	result, err := u.GetUserOneInfo(c)
	if err != nil {
		response.Failure(c, consts.CurdLoginFailCode, err.Error(), nil)
		return
	}

	var res params.GetUserInfoResponse

	res.UserInfo = result

	response.Success(c, res)
}

func GetUserList(c *gin.Context) {
	var req params.GetUserListParam

	if err := c.ShouldBind(&req); err != nil {
		response.Failure(c, consts.CurdLoginFailCode, err.Error(), nil)
		return
	}

	var u srvUser.User
	u.GetUserListParam = req
	result, total := u.GetUserList(c)

	var res params.GetUserListResponse
	res.UserList = result
	res.Total = total

	response.Success(c, res)
}

func UpdateUserInfo(c *gin.Context) {
	var req userModel.User
	if err := c.ShouldBind(&req); err != nil {
		response.Failure(c, consts.CurdLoginFailCode, err.Error(), nil)
		return
	}

	u := srvUser.User{M: req}

	err := u.Save(c)
	if err != nil {
		response.Failure(c, consts.CurdLoginFailCode, err.Error(), nil)
		return
	}

	response.Success(c, nil)
}
